<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class MFilePutController
    extends MApplicationComponent 
    implements MIController
{
    
    public function invoke($uri=null)
    {
	    set_time_limit(500);                                 parent::init();
                                if (@$_SERVER["REQUEST_METHOD"] != "POST" && @$_SERVER["REQUEST_METHOD"] != "PUT") {
            throw new MFilesException(FXP::t(MConst::REQUEST_MOTHOD_ERROR), MConst::HTTP_CODE_405);
        }
        
        $size = $_SERVER["CONTENT_LENGTH"];
        
                                if (isset($_SERVER['HTTP_IF_MATCH']) && isset($_SERVER['HTTP_X_CONTENT_LENGTH']) && isset($_SERVER['HTTP_CONTENT_RANGE'])) {
            $this->signature = $_SERVER['HTTP_IF_MATCH'];
            $btupload = new MBtUpload();
            $btupload->invoke();
            $store_path = $btupload->cache;
            $size = filesize($store_path);
        } else {
            $store_path = $this->handleStoreData($size);
        }
        
                                $url_manager = new MUrlManager();
        $path = $url_manager->parsePathFromUrl($uri);
        $root = $url_manager->parseRootFromUrl($uri);
        if ($path == false || $root == false) {
            throw new MFilesException(FXP::t(MConst::PATH_ERROR), MConst::HTTP_CODE_411);;
        }
                                $create_handler = MFilesCommon::initMFilesCommon();
        
        $path        = "/" . $path;
        $path_info   = MUtils::pathinfo_utf($path);
        $file_name   = $path_info["basename"];
        $parent_path = $path_info["dirname"];
        
        $create_handler->size           = $size;
        $create_handler->parent_path    = MUtils::convertStandardPath($parent_path);;
        $create_handler->file_name      = $file_name;
        $create_handler->root           = $root;
        $create_handler->path           = MUtils::convertStandardPath($path);
        $create_handler->type           = MUtils::mime_content_type($file_name);
                $create_handler->saveFile($store_path, $this->signature, $size, false);
                                $create_handler->SaveFileDetails();
        $create_handler->buildResult();
    }
    
    
    private function handleStoreData($size) {
                                $handle = fopen("php://input", "rb");
        $data = fread($handle, 8096);
        $tmp = DOCUMENT_TEMP . "/" . MUtils::random_string();
        if (file_exists(dirname($tmp)) === false) {
                MUtils::MkDirs(dirname($tmp));
        }
        
        $file = fopen($tmp, "wb");
        while ($data) {
            fwrite($file, $data);
            $data = fread($handle, 8096);
        }
        fclose($handle);
        fclose($file);
                                $currentSize = filesize($tmp);
        if ($currentSize != $size) {
            @unlink($tmp);
            throw new MFilesException(FXP::t("The file upload error!"), MConst::HTTP_CODE_400);
        }
        
        $this->signature = MUtils::getFileSha1($tmp);
        
                                $store_path = DOCUMENT_ROOT_BLOCK . MUtils::getPathBySplitStr($this->signature);
        if (file_exists($store_path) === false) {
                                                if (file_exists(dirname($store_path)) === false) {
                MUtils::MkDirs(dirname($store_path));
            }
            
            if (copy($tmp, $store_path) == false) {
                throw new MFilesException(FXP::t("The file upload error!"), MConst::HTTP_CODE_400);;
            }
        }
        @unlink($tmp);
        return $store_path;
    }
    
    
    
    public function handleException($exception)
    {
        parent::displayException($exception);
    }
}
?>